home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Sound / LAME / WarpOS / src / frontend / mp3rtp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-12  |  6.8 KB  |  251 lines

  1. /* $Id: mp3rtp.c,v 1.19 2001/06/08 21:29:34 markt Exp $ */
  2.  
  3. /* Still under work ..., need a client for test, where can I get one? */
  4.  
  5. /* 
  6.  *  experimental translation:
  7.  *
  8.  *  gcc -I..\include -I..\libmp3lame -o mp3rtp mp3rtp.c ../libmp3lame/libmp3lame.a lametime.c get_audio.c portableio.c ieeefloat.c timestatus.c parse.c rtp.c -lm
  9.  *
  10.  *  wavrec -t 14400 -s 44100 -S /proc/self/fd/1 | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3
  11.  */
  12.  
  13. #ifdef HAVE_CONFIG_H
  14. # include <config.h>
  15. #endif
  16.  
  17. #ifdef STDC_HEADERS
  18. # include <stdlib.h>
  19. # include <string.h>
  20. #else
  21. # ifndef HAVE_STRCHR
  22. #  define strchr index
  23. #  define strrchr rindex
  24. # endif
  25. char *strchr (), *strrchr ();
  26. # ifndef HAVE_MEMCPY
  27. #  define memcpy(d, s, n) bcopy ((s), (d), (n))
  28. #  define memmove(d, s, n) bcopy ((s), (d), (n))
  29. # endif
  30. #endif
  31.  
  32. #include <time.h>
  33.  
  34. #ifdef HAVE_UNISTD_H
  35. # include <unistd.h>
  36. #endif
  37.  
  38. #include "lame.h"
  39. #include "main.h"
  40. #include "parse.h"
  41. #include "lametime.h"
  42. #include "timestatus.h"
  43. #include "get_audio.h"
  44. #include "rtp.h"
  45.  
  46. #ifdef WITH_DMALLOC
  47. #include <dmalloc.h>
  48. #endif
  49.  
  50. #define MAX_NAME_SIZE   2048  /* current value of Linux */
  51.  
  52. /*
  53.  * Encode (via LAME) to mp3 with RTP streaming of the output.
  54.  *
  55.  * Author: Felix von Leitner <leitner@vim.org>
  56.  *
  57.  *   mp3rtp ip[:port[:ttl]] [lame encoding options] infile outfile
  58.  *
  59.  * examples:
  60.  *   arecord -b 16 -s 22050 -w | ./mp3rtp 224.17.23.42:5004:2 -b 56 - /dev/null
  61.  *   arecord -b 16 -s 44100 -w | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3
  62.  *
  63.  */
  64.  
  65. struct rtpheader    RTPheader;
  66. struct sockaddr_in  rtpsi;
  67. int                 rtpsocket;
  68.  
  69. void  rtp_output ( const char* mp3buffer, const int mp3size )
  70. {
  71.     sendrtp (rtpsocket, &rtpsi, &RTPheader, mp3buffer, mp3size);
  72.     RTPheader.timestamp += 5;
  73.     RTPheader.b.sequence++;
  74. }
  75.  
  76. #if 0
  77. struct rtpheader RTPheader;
  78. SOCKET rtpsocket;
  79.  
  80. void rtp_output (char *mp3buffer, int mp3size)
  81. {
  82.     rtp_send (rtpsocket, &RTPheader,mp3buffer,mp3size) ;
  83.     RTPheader.timestamp+=5;
  84.     RTPheader.b.sequence++;
  85. }
  86. #endif
  87.  
  88.  
  89.  
  90.  
  91. unsigned  maxvalue ( short int  Buffer [2] [1152] )
  92. {
  93.     int  max = 0;
  94.     int  i;
  95.     
  96.     for ( i = 0; i < 1152; i++ ) {
  97.         if ( abs (Buffer[0][i]) > max ) max = abs (Buffer[0][i]);
  98.         if ( abs (Buffer[1][i]) > max ) max = abs (Buffer[1][i]);
  99.     }
  100.     
  101.     return max;
  102. }
  103.  
  104. void levelmessage ( unsigned maxvalue )
  105. {
  106.     char        buff [] = "|  .  |  .  |  .  |  .  |  .  |  .  |  .  |  .  |  .  |  .  |  \r";
  107.     static int  max = 0;
  108.     static int  tmp;
  109.     
  110.     buff [tmp] = '+';
  111.     tmp = (maxvalue*61 + 16384) / (32767 + 16384/61);
  112.     if (tmp > max) max = tmp;
  113.     buff [max] = 'x';
  114.     buff [tmp] = '#';
  115.     fwrite ( buff, 1, sizeof(buff)-1, stderr );
  116. }
  117.  
  118.  
  119. /************************************************************************
  120. *
  121. * main
  122. *
  123. * PURPOSE:  MPEG-1,2 Layer III encoder with GPSYCHO 
  124. * psychoacoustic model.
  125. *
  126. ************************************************************************/
  127.  
  128. int  main ( int argc, char **argv )
  129. {
  130.     unsigned char       mp3buffer [LAME_MAXMP3BUFFER];
  131.     char       inPath    [MAX_NAME_SIZE];
  132.     char       outPath   [MAX_NAME_SIZE];
  133.     int        Buffer [2] [1152];
  134.  
  135.     lame_global_flags *gf;
  136.     
  137.     int        ret;
  138.     int        wavsamples;
  139.     int        mp3bytes;
  140.     FILE*      outf;
  141.  
  142.     char       ip [16];
  143.     unsigned   port = 5004;
  144.     unsigned   ttl  =    2;
  145.     char       dummy;
  146.  
  147.     if ( argc <= 2 ) {
  148.         fprintf ( stderr, 
  149. "Encode (via LAME) to mp3 with RTP streaming of the output\n"
  150. "\n"
  151. "    mp3rtp ip[:port[:ttl]] [lame encoding options] infile outfile\n"
  152. "\n"
  153. "    examples:\n"
  154. "      arecord -b 16 -s 22050 -w | ./mp3rtp 224.17.23.42:5004:2 -b 56 - /dev/null\n"
  155. "      arecord -b 16 -s 44100 -w | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3\n"
  156. "\n" );
  157.  
  158.     return 1;
  159.     }
  160.  
  161.     switch (sscanf ( argv[1], "%11[.0-9]:%u:%u%c", ip, &port, &ttl, &dummy )) {
  162.     case 1:
  163.     case 2:
  164.     case 3: 
  165.         break;
  166.     default:
  167.         fprintf (stderr, "Illegal destination selector '%s', must be ip[:port[:ttl]]\n", argv[1] );
  168.     return -1;
  169.     }
  170.  
  171.     rtpsocket = makesocket ( ip, port, ttl, &rtpsi );
  172.     srand ( getpid () ^ time (NULL) );
  173.     initrtp ( &RTPheader );
  174.  
  175.     /* initialize encoder */
  176.     gf=lame_init();
  177.  
  178.     /* Remove the argumets that are rtp related, and then 
  179.      * parse the command line arguments, setting various flags in the
  180.      * struct pointed to by 'gf'.  If you want to parse your own arguments,
  181.      * or call libmp3lame from a program which uses a GUI to set arguments,
  182.      * skip this call and set the values of interest in the gf struct.  
  183.      * (see lame.h for documentation about these parameters)
  184.      */
  185.      
  186.     argv[1] = argv[0]; 
  187.     parse_args(gf, argc - 1, argv + 1, inPath, outPath,NULL,NULL);
  188.  
  189.     /* open the output file.  Filename parsed into gf.inPath */
  190.     if ( 0 == strcmp ( outPath, "-" ) ) {
  191.         lame_set_stream_binary_mode (outf = stdout);
  192.     }
  193.     else {
  194.         if ((outf = fopen (outPath, "wb+")) == NULL) {
  195.             fprintf (stderr, "Could not create \"%s\".\n", outPath);
  196.             return 1;
  197.         }
  198.     }
  199.  
  200.  
  201.     /* open the wav/aiff/raw pcm or mp3 input file.  This call will
  202.      * open the file with name gf.inFile, try to parse the headers and
  203.      * set gf.samplerate, gf.num_channels, gf.num_samples.
  204.      * if you want to do your own file input, skip this call and set
  205.      * these values yourself.  
  206.      */
  207.     init_infile(gf,inPath);
  208.  
  209.  
  210.     /* Now that all the options are set, lame needs to analyze them and
  211.      * set some more options 
  212.      */
  213.     ret = lame_init_params(gf);
  214.     if ( ret < 0 ) {
  215.         if (ret == -1) display_bitrates (stderr);
  216.         fprintf (stderr, "fatal error during initialization\n");
  217.         return -1;
  218.     }
  219.  
  220.     lame_print_config(gf); /* print useful information about options being used */
  221.  
  222.     if (update_interval < 0.)
  223.         update_interval = 2.;
  224.  
  225.     /* encode until we hit EOF */
  226.     while ( (wavsamples = get_audio(gf, Buffer)) > 0 ) { /* read in 'wavsamples' samples */
  227.         levelmessage ( maxvalue (Buffer) );
  228.         mp3bytes = lame_encode_buffer_int(gf,            /* encode the frame */
  229.                                     Buffer[0], Buffer[1], wavsamples, 
  230.                     mp3buffer, sizeof (mp3buffer) );
  231.  
  232.         rtp_output ( mp3buffer, mp3bytes );       /* write MP3 output to RTP port */
  233.         fwrite ( mp3buffer, 1, mp3bytes, outf );  /* write the MP3 output to file */
  234.     }
  235.  
  236.     mp3bytes = lame_encode_flush(gf,           /* may return one or more mp3 frame */ 
  237.                                    mp3buffer, sizeof (mp3buffer) ); 
  238.     rtp_output ( mp3buffer, mp3bytes );           /* write MP3 output to RTP port */
  239.     fwrite ( mp3buffer, 1, mp3bytes, outf );      /* write the MP3 output to file */
  240.     
  241.     lame_mp3_tags_fid(gf, outf ); /* add VBR tags to mp3 file */
  242.     
  243.     lame_close(gf);
  244.     fclose(outf);
  245.     close_infile();                 /* close the sound input file */
  246.     
  247.     return 0;
  248. }
  249.  
  250. /* end of mp3rtp.c */
  251.